/*
 * @(#)AbstractAtnaStartupListener.java
 * Date 2013-11-03
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.atna.client.web;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.commons.lang3.ArrayUtils;

import us.hornerscorners.lamppost.atna.client.IAtnaClient;

import us.hornerscorners.lamppost.atna.client.constant.LoggingActor;


/**
 * Class description
 *
 *
 * @version        v1.0, 2013-11-03
 * @author         Jim Horner
 */
public abstract class AbstractAtnaStartupListener implements ServletContextListener {

    /** Field description */
    private final List<LoggingActor> actors;

    /** Field description */
    @Inject
    private IAtnaClient atnaClient;

    /**
     * Constructs ...
     *
     *
     * @param actor
     * @param actors
     */
    public AbstractAtnaStartupListener(LoggingActor actor, LoggingActor... actors) {

        super();

        this.actors = new ArrayList<>();
        this.actors.add(actor);

        if (ArrayUtils.isNotEmpty(actors)) {
            
            this.actors.addAll(Arrays.asList(actors));
        }
    }

    /**
     * Method description
     *
     *
     * @param sce
     */
    @Override
    public void contextDestroyed(ServletContextEvent sce) {

        this.atnaClient.sendApplicationStop(this.actors);
    }

    /**
     * Method description
     *
     *
     * @param sce
     */
    @Override
    public void contextInitialized(ServletContextEvent sce) {

        this.atnaClient.sendApplicationStart(this.actors);
    }
}
